#include <bits/stdc++.h>
#define int long long
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;
const int MAXN = 2e5 + 6;
const int MOD = 1e9 + 7;
int signs[4][4] = {
{1, 1, -1, -1}, {-1, 1, -1, 1}, {1, -1, 1, -1}, {-1, -1, 1, 1}};
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
}
int T = 1;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
vector<vector<int> > dp(n + 1, vector<int>(k + 1));
vector<vector<int> > costs(max(n, k) + 1, vector<int>(4));
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 4; j++) {
auto [bl, br, al, ar] = signs[j];
costs[i - 1][j] = al * a[i] + bl * b[i];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= min(i, k); j++) {
dp[i][j] = dp[i - 1][j];
for (int t = 0; t < 4; t++) {
auto [bl, br, al, ar] = signs[t];
dp[i][j] = max(dp[i][j], costs[i - j][t] + ar * a[i] + br * b[i]);
}
if (i == n) continue;
for (int t = 0; t < 4; t++) {
auto [bl, br, al, ar] = signs[t];
costs[i - j][t] =
max(costs[i - j][t], dp[i][j] + al * a[i + 1] + bl * b[i + 1]);
}
}
}
cout << dp[n][k] << "\n";
}
return 0;
}
1002. Find Common Characters | 1602A - Two Subsequences |
1555A - PizzaForces | 1607B - Odd Grasshopper |
1084A - The Fair Nut and Elevator | 1440B - Sum of Medians |
1032A - Kitchen Utensils | 1501B - Napoleon Cake |
1584B - Coloring Rectangles | 1562B - Scenes From a Memory |
1521A - Nastia and Nearly Good Numbers | 208. Implement Trie |
1605B - Reverse Sort | 1607C - Minimum Extraction |
1604B - XOR Specia-LIS-t | 1606B - Update Files |
1598B - Groups | 1602B - Divine Array |
1594B - Special Numbers | 1614A - Divan and a Store |
2085. Count Common Words With One Occurrence | 2089. Find Target Indices After Sorting Array |
2090. K Radius Subarray Averages | 2091. Removing Minimum and Maximum From Array |
6. Zigzag Conversion | 1612B - Special Permutation |
1481. Least Number of Unique Integers after K Removals | 1035. Uncrossed Lines |
328. Odd Even Linked List | 1219. Path with Maximum Gold |